<script setup>
import { ref, onMounted } from 'vue'
import { formatDate } from '@/utils/format'

// API 引入
import {
  getAdminList,
  deleteAdmin
} from '@/api/admin'

// 子组件引入（只需要一个表单组件）
import AdminDetail from './subs/AdminDetail.vue'
import AdminForm from './subs/AdminForm.vue'
import DeleteAdmin from './subs/DeleteAdmin.vue'

// 数据状态
const admins = ref([])
const queryParams = ref({
  pageNo: 1,
  size: 6,
  search: ''
})
const total = ref(0)
const loading = ref(false)

// 获取管理员列表
const adminsList = async () => {
  loading.value = true
  try {
    const response = await getAdminList(queryParams.value)
    console.log('完整响应:', response)

    if (response.code === '0' && response.data) {
      admins.value = response.data.records || []
      total.value = response.data.total || 0
    } else {
      admins.value = []
      total.value = 0
      console.error('接口返回错误:', response.msg)
    }
  } catch (err) {
    console.error('请求失败:', err)
    admins.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 分页切换
const change = (pageNo, size) => {
  queryParams.value.pageNo = pageNo
  queryParams.value.size = size
  adminsList()
}

// 搜索管理员
const submitForm = () => {
  queryParams.value.pageNo = 1
  adminsList()
}

// 详情组件引用
const adminDetailRef = ref(null)
const showDetail = (row) => {
  if (adminDetailRef.value) {
    adminDetailRef.value.openDrawer(row)
  }
}

// 表单组件引用（添加 & 编辑）
const adminFormRef = ref(null)
const addAdmin = () => {
  adminFormRef.value.openDrawer() // 传空表示添加
}
const editAdmin = (row) => {
  adminFormRef.value.openDrawer(row) // 传入 row 表示编辑
}

// 删除管理员
const deleteAdminRef = ref(null)
const handleDelete = (row) => {
  deleteAdminRef.value.open(row, deleteAdmin).then(() => {
    adminsList()
  })
}

// 页面加载时调用
onMounted(() => {
  adminsList()
})
</script>

<template>
  <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="queryParams" label-width="auto" class="demo-form-inline">
          <el-form-item label="管理员">
            <el-input placeholder="请输入管理员信息" clearable v-model="queryParams.search" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">搜</el-button>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="addAdmin">添加</el-button>
      </div>
    </template>

    <!-- 表格 -->
    <el-table :data="admins" style="width: 100%" :loading="loading">
      <el-table-column label="编号">
        <template #default="{ row }">
          <el-link type="primary" @click="showDetail(row)">{{ row.id }}</el-link>
        </template>
      </el-table-column>
      <!-- 新增图标列 -->
  <el-table-column label="图标" width="60">
    <template #default="{ row }">
      <img src="@/assets/images/OIP.webp" alt="icon" style="width:24px; height:24px;" />
    <!-- <img src="https://img.tukuppt.com/png_preview/02/93/82/PlNYHq5fmY.jpg!/fw/780" alt="icon" style="width:24px; height:24px;" /> -->
    </template>
    
  </el-table-column>
      <el-table-column prop="name" label="用户名" />
      <el-table-column prop="password" label="密码" />

      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button size="small" type="warning" @click="editAdmin(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="example-pagination-block">
      <el-pagination
        background
        layout="prev, pager, next"
        @change="change"
        :total="total"
        :page-size="queryParams.size"
      />
    </div>
  </el-card>

  <!-- 子组件引用 -->
  <admin-detail ref="adminDetailRef" />
  <admin-form ref="adminFormRef" @success="adminsList" />
  <delete-admin ref="deleteAdminRef" />
</template>

<style scoped>
.el-card {
  min-height: 100%;
  width: 100vw;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.example-pagination-block {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
